package com.xiaotu.dao;

import com.xiaotu.model.TabCommonApply;
import com.xiaotu.model.TabEnterpriseRentApplyInfo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Map;

/**
 * @作者 王艳龙
 * @日期 2021-03-10 17:08
 * @功能
 */
public interface RentApplyDao extends Mapper<TabEnterpriseRentApplyInfo> {


    /**
     * 获取企业房租通申请列表
     * @param param
     * @return
     */
    @Select("<script>"
            + "SELECT"
            + "  tra.*, tca.apply_status_desc  "
            + "FROM "
            + "  tab_enterprise_rent_apply_info tra "
            + "LEFT JOIN tab_common_apply tca ON tra.comment_apply_id = tca.apply_id where 1=1 "
            + "<if test='id !=null and id !=\"\" '>"
            + " and tra.id=#{id} "
            + "</if>"
            + "<if test='enterpriseId !=null and enterpriseId !=\"\" '>"
            + " and tra.enterprise_id=#{enterpriseId} "
            + "</if>"
            + "<if test='enterpriseName !=null and enterpriseName !=\"\" '>"
            + " and tra.enterprise_name=#{enterpriseName} "
            + "</if>"
            + "<if test='declareStartYear !=null  and declareStartYear !=\"\" and declareEndYear !=null and declareEndYear !=\"\" '>"
            + " and tra.declare_year BETWEEN #{declareStartYear} and #{declareEndYear} "
            + "</if>"
            + "<if test='startDate !=null and startDate !=\"\"  and endDate !=null and endDate !=\"\" '>"
            + " and date_format(tra.create_time,'%Y-%m-%d') BETWEEN #{startDate} and #{endDate} "
            + "</if>"
            + "<if test='applyStatusDesc !=null and applyStatusDesc !=\"\" '>"
            + " and tca.apply_status_desc =#{applyStatusDesc} "
            + "</if>"
            + " order by tra.create_time desc "
            +"</script>")
    public List<TabEnterpriseRentApplyInfo> queryRentApplyInfo(TabEnterpriseRentApplyInfo param);


    /**
     * 后台获取房租通申请
     * @param authId
     * @param approverId
     * @param enterpriseName
     * @param declareYear
     * @param applyStatusDesc
     * @return
     */
    @Select("<script>"
            + " SELECT "
            + " teu.id,teu.enterprise_name,teu.declare_year,date_format(teu.create_time,'%Y-%c-%d %h:%i:%s') as declare_date,  "
            + " temp.record_id,temp.apply_id,temp.handler_status,temp.apply_status_desc, "
            + " teur.enterprise_id AS enterpriseId,teur.enterprise_type_level1_id AS enterpriseType "
            + " FROM "
            + "  ((SELECT t1.record_id,t1.apply_id,t1.handler_status,t2.apply_status_desc "
            + " FROM tab_common_apply_record t1 "
            + " INNER JOIN tab_common_apply t2 ON t1.auth_id = t2.auth_id AND t1.apply_id = t2.apply_id "
            + " WHERE ( FIND_IN_SET( #{approverId}, t1.receiver_ids ) ) AND t1.auth_id =#{authId} "
            + " AND t1.node_id = ( IFNULL( t2.curr_node_id, 0 ) + 1 ) AND t1.handler_status = 0  "
            + " ) UNION ALL ( SELECT t1.record_id,t1.apply_id,t1.handler_status,t2.apply_status_desc "
            + " FROM tab_common_apply_record t1 INNER JOIN tab_common_apply t2 ON t1.auth_id = t2.auth_id AND t1.apply_id = t2.apply_id "
            + " WHERE t1.approver_id=#{approverId} AND t1.auth_id =#{authId} AND t2.apply_status_desc != '暂存'  "
            + " )) temp INNER JOIN tab_enterprise_rent_apply_info teu ON temp.apply_id = teu.comment_apply_id "
            + " INNER JOIN tab_enterprise_user teur ON teu.enterprise_id = teur.enterprise_id "
            + "<if test='enterpriseName !=null and enterpriseName !=\"\" '>"
            + " and teu.enterprise_name LIKE CONCAT(CONCAT('%', #{enterpriseName}), '%') "
            + "</if>"
            + "<if test='declareYear !=null and declareYear !=\"\" '>"
            + " and teu.declare_year=#{declareYear} "
            + "</if>"
            + "<if test='applyStatusDesc !=null and applyStatusDesc !=\"\" '>"
            + " and temp.apply_status_desc =#{applyStatusDesc} "
            + "</if>"
            + " ORDER BY teu.create_time desc "
            +"</script>")
    public List<Map<String,Object>> queryRentApplyByAuthId(@Param("authId") String authId,@Param("approverId") String approverId,
         @Param("enterpriseName") String enterpriseName,@Param("declareYear")  String declareYear,@Param("applyStatusDesc")  String applyStatusDesc);

}
